package com.lotsrc.baicao.module.sys.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.AppUtils;
import com.king.app.dialog.AppDialog;
import com.king.app.dialog.AppDialogConfig;
import com.king.app.updater.AppUpdater;
import com.lotsrc.appbase.base.BaseFragment;
import com.lotsrc.baicao.R;
import com.lotsrc.baicao.databinding.FragmentSysSoftupdateBinding;
import com.lotsrc.baicao.databinding.FragmentTemplateBinding;
import com.lotsrc.baicao.net.HttpCommonManager;
import com.lotsrc.zuilib.listener.ZListenerHttp;

import java.io.IOException;

public class SoftUpdateFragment extends BaseFragment {

    private String TAG="SoftUpdateFragment";
    private FragmentSysSoftupdateBinding binding;
    private AppUpdater mAppUpdater;

    //请求版本地址
    String requestUrl="https://lotsrc.oss-cn-hangzhou.aliyuncs.com/vme/apk/baicao/appversion.txt";
    //APP更新
    private String remark="";
    //APP下载地址
    private String appAddress="";


    public static SoftUpdateFragment newInstance() {
        SoftUpdateFragment fragment = new SoftUpdateFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding=FragmentSysSoftupdateBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println(TAG+"-onViewCreated");
        initView();
        initViewEvent();
        checkUpdate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println(TAG+"-onDestroyView");
    }

    @Override
    public void initView() {
        binding.tvCurrentVersion.setText(AppUtils.getAppVersionName());
        binding.tvUpdateInfo.setText("正在检查更新...");
    }

    @Override
    public void initViewEvent() {
        binding.ivBack.setOnClickListener(v -> {
            getActivity().finish();
        });
    }

    @Override
    public void initObserve() {

    }

    //检查更新
    private void checkUpdate(){
        HttpCommonManager httpCommonManager=new HttpCommonManager();
        httpCommonManager.setListener(new ZListenerHttp() {
            @Override
            public void onFailure(IOException ex) {

            }

            @Override
            public void onResponse(String body) {
                System.out.println("body:"+body);
                JSONObject jsonObject= JSON.parseObject(body);
                JSONObject data=jsonObject.getJSONObject("data");
                int versionCode=data.getIntValue("versionCode");
                String versionName=data.getString("versionName");
                appAddress=data.getString("appAddress");
                remark=data.getString("remark");
                checkIsLastVersion(versionCode,versionName);
            }
        });
        httpCommonManager.request(requestUrl);
    }

    //检查是否为最新版本
    private void checkIsLastVersion(int serverVersionCode,String serverVersionName){
        int currentVersion= AppUtils.getAppVersionCode();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(serverVersionCode>currentVersion){
                    binding.tvCurrentVersion.setText(AppUtils.getAppVersionName());
                    binding.tvUpdateInfo.setText("正在更新...");
                    startUpdate();
                }else{
                    binding.tvUpdateInfo.setText("已经是最新版本");
                }
            }
        });
    }

    //开始升级
    private void startUpdate(){
        AppDialogConfig config = new AppDialogConfig(getContext());
        config.setTitle("软件升级")
                .setConfirm("升级")
                .setContent(remark)
                .setOnClickConfirm(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAppUpdater = new AppUpdater(getContext(), appAddress);
                        mAppUpdater.start();
                        AppDialog.INSTANCE.dismissDialog();
                    }
                });
        AppDialog.INSTANCE.showDialog(config);
    }
}